package com.ffcs.crmd.tsp.admin.control;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.exception.ExceptionUtil;
import com.ffcs.crmd.platform.pub.vo.RetVo;
import com.ffcs.crmd.tsp.admin.api.dto.JobDetailDTO;
import com.ffcs.crmd.tsp.admin.api.dto.JobDetailRetryDTO;
import com.ffcs.crmd.tsp.admin.api.dto.JobDetailRunningDTO;
import com.ffcs.crmd.tsp.admin.api.dto.JobDetailThreadInfoDTO;
import com.ffcs.crmd.tsp.admin.api.facade.IJobDetailFacade;

/**
 * 
 * 功能说明:子任务控制层
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年2月16日 下午4:49:00
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
@Controller
@ResponseBody
@RequestMapping("/tsp/jobDetail")
public class JobDetailController {
    
    @Autowired
    private IJobDetailFacade jobDetailFacade;
    
    private final Logger     logger = LoggerFactory.getLogger(JobDetailController.class);
    
    @RequestMapping(value = "/add")
    public RetVo add(ModelMap map, HttpSession httpSession, @RequestBody JobDetailDTO jobDetailDto) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.save(jobDetailDto);
        } catch (Exception e) {
            logger.error("JobDetailController : add job detail exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    @RequestMapping(value = "/delete")
    public RetVo delete(ModelMap map, HttpSession httpSession, @RequestBody JobDetailDTO jobDetailDto) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.remove(jobDetailDto);
        } catch (Exception e) {
            logger.error("JobDetailController : delete job details exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    @RequestMapping(value = "/deleteList")
    public RetVo deleteList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDetailDTO> jobDetailDtoList) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.remove(jobDetailDtoList);
        } catch (Exception e) {
            logger.error("JobDetailController : delete job details exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/finish")
    public RetVo finish(ModelMap map, HttpSession httpSession, @RequestBody JobDetailRunningDTO jobDetailRunningDTO) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.finish(jobDetailRunningDTO);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/finishList")
    public RetVo finishList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDetailRunningDTO> jobDetailRunningDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.finish(jobDetailRunningDTOList);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/get")
    public RetVo get(ModelMap map, HttpSession httpSession, @RequestBody JobDetailDTO jobDetailDTO) {
        RetVo retVo = new RetVo(true);
        try {
            JobDetailDTO rjobDetailDTO = jobDetailFacade.get(jobDetailDTO);
            retVo.setObject(rjobDetailDTO);
        } catch (Exception e) {
            logger.error("Job Action : add job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/interrupt")
    public RetVo interrupt(ModelMap map, HttpSession httpSession, @RequestBody JobDetailRunningDTO jobDetailRunningDTO) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.interrupt(jobDetailRunningDTO);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/interruptList")
    public RetVo interruptList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDetailRunningDTO> jobDetailRunningDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.interrupt(jobDetailRunningDTOList);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/pause")
    public RetVo pause(ModelMap map, HttpSession httpSession, @RequestBody JobDetailDTO jobDetailDto) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.pause(jobDetailDto);
        } catch (Exception e) {
            logger.error("JobDetailController : pause job details exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/pauseList")
    public RetVo pauseList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDetailDTO> jobDetailDtoList) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.pause(jobDetailDtoList);
        } catch (Exception e) {
            logger.error("JobDetailController : pause job details exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/queryPage")
    public RetVo queryPage(ModelMap map, HttpSession httpSession, @RequestBody JobDetailDTO jobDetailDto) {
        RetVo retVo = new RetVo(true);
        try {
            if(StringUtils.isBlank(jobDetailDto.getClusterName())){
                String clusterName = httpSession.getAttribute("clusterName") == null ? null
                    : String.valueOf(httpSession.getAttribute("clusterName"));
                jobDetailDto.setClusterName(clusterName);
            }
            PageInfo<JobDetailDTO> pageInfo = jobDetailFacade.queryPage(jobDetailDto);
            retVo.setPageInfo(pageInfo);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/queryRetryPage")
    public RetVo queryRetryPage(ModelMap map, HttpSession httpSession, @RequestBody JobDetailRetryDTO jobDetailRetryDTO) {
        RetVo retVo = new RetVo(true);
        try {
            PageInfo<JobDetailRetryDTO> pageInfo = jobDetailFacade.queryRetryPage(jobDetailRetryDTO);
            retVo.setPageInfo(pageInfo);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    @RequestMapping(value = "/queryRunningPage")
    public RetVo queryRunningPage(ModelMap map, HttpSession httpSession, @RequestBody JobDetailRunningDTO jobDetailRunningDto) {
        RetVo retVo = new RetVo(true);
        try {
            PageInfo<JobDetailRunningDTO> pageInfo = jobDetailFacade.queryRunningPage(jobDetailRunningDto);
            retVo.setPageInfo(pageInfo);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    @RequestMapping(value = "/queryThreadInfo")
    public RetVo queryThreadInfo(ModelMap map, HttpSession httpSession, @RequestBody JobDetailRunningDTO jobDetailRunningDTO) {
        RetVo retVo = new RetVo(true);
        try {
            JobDetailThreadInfoDTO jobDetailThreadInfoDTO = jobDetailFacade.queryThreadInfo(jobDetailRunningDTO);
            retVo.setObject(jobDetailThreadInfoDTO);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/reset")
    public RetVo reset(ModelMap map, HttpSession httpSession, @RequestBody JobDetailRunningDTO jobDetailRunningDTO) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.reset(jobDetailRunningDTO);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/resetList")
    public RetVo resetList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDetailRunningDTO> jobDetailRunningDTOList) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.reset(jobDetailRunningDTOList);
        } catch (Exception e) {
            logger.error("JobDetailController : query job detail page by job exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/resume")
    public RetVo resume(ModelMap map, HttpSession httpSession, @RequestBody JobDetailDTO jobDetailDto) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.resume(jobDetailDto);
        } catch (Exception e) {
            logger.error("JobDetailController : resume job details exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    @RequestMapping(value = "/resumeList")
    public RetVo resumeList(ModelMap map, HttpSession httpSession, @RequestBody List<JobDetailDTO> jobDetailDtoList) {
        RetVo retVo = new RetVo(true);
        try {
            jobDetailFacade.resume(jobDetailDtoList);
        } catch (Exception e) {
            logger.error("JobDetailController : resume job details exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
}
